﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using GravityBallScreenSaver.Gravity;
using GravityBallScreenSaver.Graphics;

namespace GravityBallScreenSaver
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class GravityBall : Window
    {
        Gravity.IGravity physicsController; 

        public GravityBall()
        {
            InitializeComponent();

            physicsController = new Gravity.Gravity(1, 1);

            Initalize();
        }

        public GravityBall(int width, int height)
        {
            InitializeComponent();

            physicsController = new Gravity.Gravity(width, height);

            AGraphicsEngine visualHost = new GraphicsEngine(width, height, physicsController);
            grid1.Children.Add(visualHost);            

            Initalize();
        }

        public void Initalize()
        {                        
            physicsController.Step += OnGravityStep;

            //User timer inside of physics controller to animate the planets
            physicsController.Start();

        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void OnGravityStep(object source, GravityEventArgs e)
        {
            //grid1.Children.Clear();
            //AGraphicsEngine visualHost = new GraphicsEngine(1, 1, physicsController);
            //grid1.Children.Add(visualHost);
            grid1.InvalidateVisual();
            grid1.Children[0] = new GraphicsEngine(1, 1, physicsController);
        }

        // This delegate enables asynchronous calls for setting
        // the text property on a TextBox control.
        delegate void SetVisualCallback();

        private void ResetGrid()
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.grid1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.textBox1.Text = text;
            }
        }

    }
}
